package com.didi.gulimall.ware.service.impl;

import com.didi.gulimall.api.ProductApiService;
import com.didi.gulimall.common.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.didi.gulimall.common.utils.PageUtils;
import com.didi.gulimall.common.utils.Query;

import com.didi.gulimall.ware.dao.PurchaseDetailDao;
import com.didi.gulimall.ware.entity.PurchaseDetailEntity;
import com.didi.gulimall.ware.service.PurchaseDetailService;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {
    @Autowired
    private ProductApiService productApiService;
    /**
     * key: '华为',//检索关键字
     * status: 0,//状态
     * wareId: 1,//仓库id
     *
     * @param params
     * @return
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            //purchase_id  sku_id
            queryWrapper.and(w -> {
                w.eq("purchase_id", key).or().eq("sku_id", key);
            });
        }
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            //purchase_id  sku_id
            queryWrapper.eq("status", status);
        }
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            //purchase_id  sku_id
            queryWrapper.eq("ware_id", wareId);
        }
        IPage<PurchaseDetailEntity> page = this.page(
                new Query<PurchaseDetailEntity>().getPage(params),
                queryWrapper
        );
        return new PageUtils(page);
    }

    @Override
    public void savePurchaseDetail(PurchaseDetailEntity purchaseDetail) {
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .eq("sku_id",purchaseDetail.getSkuId())
                .eq("ware_id",purchaseDetail.getWareId());
        PurchaseDetailEntity one = this.getOne(queryWrapper);
        if (one==null){
            R info = productApiService.info(purchaseDetail.getSkuId());
            Map<String, Object> data = (Map<String, Object>) info.get("skuInfo");
            if (info.getCode() == 0) {
                Double skuPrice = (Double) data.get("price");
                purchaseDetail.setSkuPrice(new BigDecimal(skuPrice));
            }
            this.save(purchaseDetail);
        }else {
            one.setSkuNum(purchaseDetail.getSkuNum()+one.getSkuNum());
            this.updateById(one);
        }

    }

    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        return this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
    }
}